package gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.accessibility.AccessibleComponent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import javax.swing.WindowConstants;

import model.basics.Request;
import model.time.IllegalTimeValuesException;
import model.time.InvalidMinutesException;
import model.time.NullParametersReceivedExpeption;

import org.jdesktop.application.Application;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextPane;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AssignmentPanel extends javax.swing.JPanel {
	private JScrollPane requestScroll;
	private JButton generateOrderButton;
	private JButton modifyButton;
	private JButton dropButton;
	private JList requestList;
	private JButton cancelButton;
	private JSeparator jSeparator1;
	private JLabel jLabel1;
	private JTextPane dataPane;
	private JScrollPane dataScroll;
	private model.search.System sistem;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	 * @throws InvalidMinutesException 
	 * @throws IllegalTimeValuesException 
	 * @throws NullParametersReceivedExpeption 
	*/
	public static void main(String[] args) throws NullParametersReceivedExpeption, IllegalTimeValuesException, InvalidMinutesException {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new AssignmentPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public AssignmentPanel() throws NullParametersReceivedExpeption, IllegalTimeValuesException, InvalidMinutesException {
		super();
		sistem = new model.search.System();
		sistem.setRequests();
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(416, 427));
			this.setLayout(null);
			{
				requestScroll = new JScrollPane();
				this.add(requestScroll);
				requestScroll.setBounds(14, 59, 190, 317);
				{
					ListModel requestListModel = 
						new DefaultComboBoxModel(
								new String[] {});
					requestList = new JList();
					requestScroll.setViewportView(requestList);
					requestList.setModel(requestListModel);
					requestList.setListData((Vector<Request>)(sistem.getReqs()));
					requestList.addListSelectionListener(new InfoListener());
				}
			}
			{
				dataScroll = new JScrollPane();
				this.add(dataScroll);
				dataScroll.setBounds(210, 59, 192, 317);
				{
					dataPane = new JTextPane();
					dataPane.setEditable(false);
					dataScroll.setViewportView(dataPane);
					dataPane.setName("dataPane");
				}
			}
			{
				dropButton = new JButton();
				this.add(dropButton);
				dropButton.setBounds(12, 388, 80, 28);
				dropButton.setName("dropButton");
				dropButton.addActionListener(new DropRequest());
			}
			{
				modifyButton = new JButton();
				this.add(modifyButton);
				modifyButton.setBounds(115, 388, 80, 28);
				modifyButton.setName("modifyButton");
				modifyButton.addActionListener(new ModifyRequest());
			}
			{
				generateOrderButton = new JButton();
				this.add(generateOrderButton);
				generateOrderButton.setBounds(218, 388, 80, 28);
				generateOrderButton.setName("generateOrderButton");
			}
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setBounds(14, 18, 342, 24);
				jLabel1.setName("jLabel1");
			}
			{
				jSeparator1 = new JSeparator();
				this.add(jSeparator1);
				jSeparator1.setBounds(14, 46, 269, 10);
			}
			{
				cancelButton = new JButton();
				this.add(cancelButton);
				cancelButton.setBounds(322, 388, 80, 28);
				cancelButton.setName("cancelButton");
			}
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private class InfoListener implements ListSelectionListener{
		
		public void valueChanged(ListSelectionEvent e) {
			// TODO Auto-generated method stub
			if (requestList.hasFocus()){
				Request k = (Request) requestList.getSelectedValue();
				dataPane.setText(k.toShow());
			}
		}
		
	}
	
	private class DropRequest implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
				sistem.removeRequest((Request) requestList.getSelectedValue());
				requestList.setListData((Vector<Request>) sistem.getReqs());
				dataPane.setText("");
		}
		
	}
	
	private class ModifyRequest implements ActionListener{
		
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
			RequestP rp = new RequestP((Request) requestList.getSelectedValue());
			rp.getRequestW().getInstructorPane().setText(((Request) requestList.getSelectedValue()).getInstructor().toStrings());
			rp.setVisible(true);
			dataPane.setText("");
		}
		
	}

}
